package com.news.service.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.news.entity.Content;
import com.news.entity.Gather;
import com.news.mapper.ContentMapper;
import com.news.service.ContentService;
import com.utils.HtmlParser;
import com.utils.TmStringUtils;

@Transactional(rollbackFor=Exception.class)
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements ContentService{

	@Autowired
	ContentMapper contentMapper;
	
	@Override
	public List<Content> getContentList(Page<Content> page, Content view) {
		page.setRecords(contentMapper.getContentList(page,view));
		return page.getRecords();
	}

	@Override
	public String getHtmlSource(Gather gather) {
		//获取网页源代码
		String htmlsource = HtmlParser.getHtmlSource(gather.getUrl(),gather.getCharset());
		Document document = Jsoup.parse(htmlsource);
		Set<String> set = new HashSet<String>();
		Elements elements = document.getElementsByTag("a");
		for (Element element : elements) {
			String href = element.attr("href");
			if(TmStringUtils.isNotEmpty(href)&& href.startsWith(gather.getPrefix())){
				set.add(href);
			}
		}
		
		//URL地址循环，进行抓取
		for (String url:set) {
			try {
				Document doc = Jsoup.connect(url).get();
				if(doc!=null){
					
					//抓取标题
					String title = null;
					if(gather.getTitleMark()==1){
						title= doc.title();
					}else if(gather.getTitleMark()==2){
						title= doc.getElementsByTag(gather.getTitleRegex()).eq(0).text();
					}else if(gather.getTitleMark()==3){
						title= doc.getElementsByClass(gather.getTitleRegex()).eq(0).text();
					}
					
					
					//抓取SEO关键字
					String keywords ="";
					//抓取SEO内容
					String description="";
					Elements metas = doc.head().select("meta");
					for (Element meta : metas) {
						String content = meta.attr("content");
						if ("keywords".equalsIgnoreCase(meta.attr("name"))) {
							keywords = content;
						}
						if ("description".equalsIgnoreCase(meta.attr("name"))) {
							description = content;
						}
					}
					
					//抓取新闻内容
					Element contentDom = null;
					if(gather.getContentMark()==1){
						contentDom= doc.getElementById(gather.getContentRegex());
					}else if(gather.getContentMark()==2){
						contentDom= doc.getElementsByTag(gather.getContentRegex()).first();
					}else if(gather.getContentMark()==3){
						contentDom= doc.getElementsByClass(gather.getTitleRegex()).first();
					}
					
					
					String content = contentDom.html();
					//抓取图片
					String img = contentDom.getElementsByTag("img").eq(0).attr("src");
					//抓取时间
					String tag = doc.getElementsByClass("post_time_source").eq(0).text();
					
					
					Content newContent = new Content();
					setContentData(newContent);
					newContent.setTitle(title);
					newContent.setContent(content);
					newContent.setImg(img);
					newContent.setKeywords(keywords);
					newContent.setDescription(description);
					newContent.setTag(tag);
					newContent.setCreateTime(new Date());
					Integer num=contentMapper.insert(newContent);
					if(num>0) {
						return "success";
					}
				}
				
			} catch (Exception e) {
				continue;
			}
		}
		
		return null;
	}
	
	
	private void setContentData(Content content){
		content.setStaticLink(null);
		content.setIsTop(0);
		content.setIsDelete(0);
		content.setStatus(0);
		content.setType(1);	
		content.setPush(0);
		content.setIsComment(1);
		content.setHits(0);
		content.setLoves(0);
		content.setCollections(0);
		content.setHtmlCode("");
		content.setJsCode("");
		content.setCssCode("");
		content.setIsCode(0);
	}

}
